/**
 * 仅用于渲染进程
 */

const {animation_support} = require('./mainlib/window-animation');
const childProcess = require('child_process');

const ofs = require('original-fs');
const path = require('path');
const fs = require('fs');
const {remote} = require('electron');
const { Menu, MenuItem, BrowserWindow, app} = remote;

const core = require('./mainlib/core');
const Type = require('./mainlib/type');

let windows = {};
let windowOptions = {
    setting: {
        width: 800,
        height: 600,
        show:false,
        frame:false,
        resizable: !core.isInAsar(),
        title: '设置',
        webPreferences: {
            nodeIntegration: true,
        },
        
        src: path.join(__dirname, 'window\\setting.html')
    },
    
    about: {
        width: 400,
        height: 270,
        show: false,
        frame: false,
        resizable: !core.isInAsar(),
        minimizable:false,
    
        title: '关于',
        webPreferences: {
            nodeIntegration: true,
        },
    
        src: path.join(__dirname, 'window\\about.html')
    },
    
    changelog: {
        width: 400,
        height: 600,
        show: false,
        frame: false,
        resizable: !core.isInAsar(),
        minimizable:false,
    
        title: '更新日志',
        webPreferences: {
            nodeIntegration: true,
        },
    
        src: path.join(__dirname, 'window\\changelog.html')
    }, 
    
    searchingThread: {
        width: 1000,
        height: 700,
        show: false,
        frame: false,
        resizable: true,
        minimizable: true,

        title: 'searchingThread',
        webPreferences: {
            nodeIntegration: true,
        },
        //
        // src: path.join(__dirname, 'window\\changelog.html')
    }
};

/**
 * 窗口管理器
 * @type {{launch(*, *): void, open(*, *=): void, listen(): void}}
 */
const WindowManager = {
    open(name, newOptions) {
        newOptions = newOptions ? newOptions : {};
        
        if(windows[name]) {
            windows[name].showWithAnimation();
        } else {
            if(windowOptions[name]) {
                let windowOption = Type.cloneMerge(windowOptions[name], newOptions);
                let src = windowOption.src;
                delete windowOption.src;
                
                let win = new BrowserWindow(windowOption);
                windows[name] = win;
                animation_support(win);
    
                win.loadFile(src);
    
                if(core.isInAsar()) {
                    win.removeMenu();
                }

                win.on('closed', () => {
                    delete windows[name];
                });
    
                win.once('ready-to-show', () => {
                    win.showWithAnimation();
                });
            }
        }
    },
    
    launch(name, scriptFile) {
        if(windows[name]) {
            return windows[name];
        } else {
            if(windowOptions[name]) {
                let windowOption = Type.cloneMerge(windowOptions[name], {});
                let src = windowOption.src;
                delete windowOption.src;

                let win = new BrowserWindow(windowOption);
                windows[name] = win;
                // animation_support(win);

                if(src) {
                    win.loadFile(src);
                }

                win.on('closed', () => {
                    delete windows[name];
                });

                win.once('ready-to-show', () => {
                    win.webContents.executeJavaScript(`document.write('<script src="${scriptFile}" type="text/javascript">)`);
                });
                
                return windows[name];
            } else {
                return null;
            }
        }
    },
    
    listen() {
        core.on(core.ET.OPEN_WINDOW, (args) => {
            this.open(args.name, args.options);
        });

        core.on(core.ET.LAUNCH_WINDOW, (args) => {
            this.launch(args.name, args.scriptFile);
        });
    },
    
};

module.exports = WindowManager;
